package com.EstateManager.Controller;

import java.io.Console;
import java.io.File;
import java.io.IOException;
import java.rmi.dgc.Lease;
import java.security.PublicKey;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.enterprise.inject.New;
import javax.faces.view.facelets.Facelet;
import javax.json.JsonString;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.plaf.ScrollPaneUI;
import javax.swing.plaf.multi.MultiTabbedPaneUI;
import javax.validation.Valid;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.io.filefilter.FalseFileFilter;
import org.hibernate.HibernateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.EstateManager.Dao.AdminDao;
import com.EstateManager.Dao.EstateBasicInfoDao;
import com.EstateManager.Dao.EstateInfoFileDao;
import com.EstateManager.Dao.EstateSchedualDao;
import com.EstateManager.Dao.LeaseOrSaleDao;
import com.EstateManager.Dao.LeaseOrSaleRecoldDao;
import com.EstateManager.Dao.MultiTableDao;
import com.EstateManager.Dao.UserDao;
import com.EstateManager.Entity.Estatebasicinfo;
import com.EstateManager.Entity.Estateinfofile;
import com.EstateManager.Entity.Estateschedual;
import com.EstateManager.Entity.Leaseorsaleinfo;
import com.EstateManager.Entity.Leaseorsalerecords;
import com.EstateManager.Entity.User;
import com.EstateManager.Test.MainTest;
import com.EstateManager.Utils.CommenUtils;
import com.EstateManager.Utils.FactoryResult;
import com.EstateManager.Utils.Result;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

@Controller
@RequestMapping("user")
public class UserController {
	
	@Autowired
	private MainTest mainTest;
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private EstateBasicInfoDao estateBasicInfoDao;
	
	@Autowired
	private EstateInfoFileDao estateInfoFileDao;
	
	@Autowired
	private LeaseOrSaleDao leaseOrSaleDao;
	
	@Autowired
	private EstateSchedualDao estateSchedualDao;
	
	@Autowired
	private MultiTableDao multiTableDao;
	
	@Autowired
	private LeaseOrSaleRecoldDao leaseOrSaleRecoldDao;
	
	/**
	 * 返回用户登录页面
	 * 访问路径: /EstateManager/user/login.do
	 * method : get
	 * */
	@RequestMapping(value = "login", method = RequestMethod.GET)
	public String Login() {
	
		return "/User/login";
	
	}
	
	/**
	 * 验证用户登录 <br>
	 * 访问路径: /EstateManager/user/check-login.do<br>
	 * method: post<br>
	 * @param userName 用户名
	 * @param password 密码
	 * @return result 登陆成功 :{"code":"1000","message":"用户登录成功","data":user}
	 * 				      登陆失败 :{"code":"4000","message":"用户不存在","data":""}
	 * 传入session : {"userName":'',"userId":''};
	 * 
	 * */
	@RequestMapping(value = "check-login", method = RequestMethod.POST)
	@ResponseBody
	public Result loginCheck(String userName , String password , HttpServletRequest request , HttpServletResponse response){
		User user = userDao.getUserByUsername(userName);
		Result result;
		if (user == null || !user.getPassword().equals(password)) {
			result = FactoryResult.getResult(FactoryResult.ERROR, null, "用户名或密码错误");
		}
		else {
			request.getSession().setAttribute("userName", user.getUsername());
			request.getSession().setAttribute("userId", user.getId());
			result = FactoryResult.getResult(FactoryResult.SUCCESS, user, "用户登录成功");
		}
		return result;
	}
	
	/**
	 * 
	 * 显示用户主页
	 * 访问路径: /EstateManager/user/view-PersonalHome.do
	 * method : get
	 * 
	 * */
	@RequestMapping(value = "view-PersonalHome", method = RequestMethod.GET)
	public String showPersonInfo(){
		return "/User/personal";
	}
	
	/**
	 * 获取当前用户信息管理
	 * method: GET
	 * 访问路径 : /EstateManager/user/view-userInfoManager.do
	 * */
	@RequestMapping(value="view-userInfoManager", method = RequestMethod.GET)
	public String userInfoManager(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		User user = userDao.get((Integer)request.getSession().getAttribute("userId"));
		model.addAttribute("user", user);
		return "/User/userInfoManager";
		
	}

	/**
	 * 检验昵称是否存在
	 * 访问路径: /EstateManager/user/json-isNickNameExist.do
	 * method : post
	 * @param : param 昵称
	 * @return : {"info": "昵称已存在","status": "n","data": user,"code": 4000,"message": "昵称已存在"}
	 * 			 {"info": "昵称不存在","status": "y","data": "","code": 1000,"message": "昵称不存在"}
	 * */
	@RequestMapping(value = "json-isNickNameExist", method = RequestMethod.POST)
	@ResponseBody
	public Result isNickNameExist(HttpServletRequest request, String param){
		User user = userDao.getUserByNickName(param);
		Result result;
		if (user != null && user.getId() != (int)request.getSession().getAttribute("userId")) {
			result = FactoryResult.getResult(FactoryResult.ERROR, null, "昵称已存在");
		}else {
			result = FactoryResult.getResult(FactoryResult.SUCCESS,null, "昵称可用");
		}
		return result;
	}

	/**
	 * 用户信息修改<br>
	 * Method : POST
	 * URL : /EstateManager/user/json-userInfoModify.do
	 * @param user 用户信息
	 * @return result {"code": 1000,"data": user, "info": "用户数据修改成功", "status": "y"}
	 * 				  {"code": 4000,"data": e, "info": "用户数据修改失败", "status": "n"}
	 * <br>
	 * 
	 * 
	 * */
	@SuppressWarnings("finally")
	@RequestMapping(value = "json-userInfoModify", method = RequestMethod.POST)
	@ResponseBody
	public Result userInfoModify(@ModelAttribute("user") @Valid User user){
		Result result;
		try {
			user.setPassword(userDao.get(user.getId()).getPassword());
			userDao.merge(user);
			result = FactoryResult.getResult(FactoryResult.SUCCESS, user, "用户数据修改成功");
		} catch (HibernateException e) {
			result = FactoryResult.getResult(FactoryResult.EXCEPTION, e, "用户数据修改失败");
		} 
		return result;
	}
	
	/**
	 * 请求密码修改页面
	 * URL: /EstateManager/user/view-passwordModify.do
	 * Method: GET
	 * */
	@RequestMapping(value = "view-passwordModify", method = RequestMethod.GET)
	public String passwordModifyView(){
		return "/User/PasswordModify";
	}
	
	/**
	 * 对密码进行修改
	 * URL : /EstateManager/user/json-passwordModify.do
	 * Method : POST
	 * 
	 * @param oldPassword 原密码
	 * @param newPassword 新密码[需大于六位]
	 * 
	 * @return result 
	 * 
	 * */
	@RequestMapping(value = "json-passwordModify", method = RequestMethod.POST)
	@ResponseBody
	public Result passwordModify(HttpServletRequest request, String oldPassword, String newPassword) {
		
		int userId = (int)request.getSession().getAttribute("userId");
		Result result;
 		User user = userDao.get(userId);
		if (user.getPassword().equals(oldPassword) && newPassword.length() > 6) {
			user.setPassword(newPassword);
			try {
				userDao.merge(user);
				result = FactoryResult.getResult(FactoryResult.SUCCESS, user.getPassword(), "密码修改成功");
			} catch (HibernateException e) {
				result = FactoryResult.getResult(FactoryResult.EXCEPTION, e, "密码修改失败");
				e.printStackTrace(System.out);
			}
		}else {
			result = FactoryResult.getResult(FactoryResult.OTHERERROR, null, "输入信息有误[密码错误或新密码长度小于6位]");
		}
		return result;
	}
	
	/**
	 * 请求密码修改页面
	 * URL: /EstateManager/user/view-addEstateInfo.do
	 * Method: GET
	 * */
	@RequestMapping(value = "view-addEstateInfo", method = RequestMethod.GET)
	public String addEstateInfoView(){
		return "/User/addEstateInfo";
	}
	
	/**
	 * 添加房产信息验证
	 * URL: /EstateManager/user/json-addEstateInfo.do
	 * Method: POST  
	 * @param
	 *   
	 **/
	@RequestMapping(value = "json-addEstateInfo", method = RequestMethod.POST)	
	@ResponseBody
	public Result addEstateInfoJson(@RequestParam(value = "file", required = false) MultipartFile file, DefaultMultipartHttpServletRequest request) {
		Estatebasicinfo estatebasicinfo;
		Map<String, Object> parame = new HashMap<String, Object>();
		Enumeration<String> parameNames = request.getParameterNames();
		while (parameNames.hasMoreElements()) {
			String name = parameNames.nextElement();
			parame.put(name, request.getParameter(name));
		}
		
		Result result;
		String path = request.getSession().getServletContext().getRealPath("/");
		String fileName = file.getOriginalFilename();
		
		fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + fileName;
		System.out.println(path);
		try {
			estatebasicinfo = (Estatebasicinfo)CommenUtils.mapToObject(parame, Estatebasicinfo.class);
			estatebasicinfo.setUser(new User());
			estatebasicinfo.getUser().setId(Integer.parseInt(String.valueOf(parame.get("user.id"))));
			estatebasicinfo.setPublicStatu(EstateBasicInfoDao.PUBCLICSTATUE_NOPUBLIC);
			int id = estateBasicInfoDao.save(estatebasicinfo);
			String fileDir =  "Content\\file\\" + id;
			File targetFile = new File(path + fileDir, fileName);
			if (!targetFile.exists()) {
				targetFile.mkdirs();
			}
			file.transferTo(targetFile);
			estatebasicinfo.setId(id);
			
			Estateinfofile estateinfofile = new Estateinfofile(estatebasicinfo, fileDir.replaceAll("\\\\", "/") + "/" + fileName, EstateInfoFileDao.FILE_CHECK,"系统描述:房产证明信息");
			estateInfoFileDao.save(estateinfofile);
			result = FactoryResult.getResult(FactoryResult.SUCCESS, id, "房产信息插入成功");
		} catch (Exception e) {
			e.printStackTrace(System.out);
			result = FactoryResult.getResult(FactoryResult.EXCEPTION, e, "房产信息插入失败");
		}
		return result; 
	}
	
	/**
	 * 添加房产信息文件
	 * URL: /EstateManager/user/json-addEstateFile.do
	 * Method: POST  
	 * @param
	 *   
	 **/
	@RequestMapping(value = "json-addEstateFile")
	@ResponseBody
	public Result addEstateFile(@RequestParam(value = "files", required = false) MultipartFile[] files, DefaultMultipartHttpServletRequest request, Integer estateBasicId){
			Result result;
			if (estateBasicId == null) {
				estateBasicId = Integer.parseInt(request.getParameter("estateBasicId"));
			}
			String path = request.getSession().getServletContext().getRealPath("/");
			String fileDir =  "Content\\file\\" + estateBasicId;
			if (files != null) {
				try {
					for (MultipartFile file : files) {
						String fileName = file.getOriginalFilename();
						fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + fileName;
						File targetFile = new File(path + fileDir, fileName);
						Estatebasicinfo estatebasicinfo = new Estatebasicinfo();
						estatebasicinfo.setId(estateBasicId);
						if (!targetFile.exists()) {
							targetFile.mkdirs();
						}
						file.transferTo(targetFile);
						Estateinfofile estateinfofile = new Estateinfofile(estatebasicinfo, fileDir.replaceAll("\\\\", "/") + "/" + fileName, EstateInfoFileDao.FILE_DESCRIB,"系统描述:房产發佈描述信息文件");
						estateInfoFileDao.save(estateinfofile);
					}
					result = FactoryResult.getResult(FactoryResult.SUCCESS, null, "房产信息文件上傳成功");
				} catch (Exception e) {
					e.printStackTrace(System.out);
					result = FactoryResult.getResult(FactoryResult.EXCEPTION, e, "房产信息文件上傳成失败");
				} 
			}else {
				result  = FactoryResult.getResult(FactoryResult.ERROR, null, "房产信息文件不存在");
			}
			return result;
		
	}
	
	/**
	 * 返回用户房产信息页面
	 * url : /EstateManager/user/view-personalEstate.do
	 * method : get
	 * */
	@RequestMapping(value = "view-personalEstate", method = RequestMethod.GET)
	public String personalEstate(){
		return "User/personalEstate";
	}
	
	/**
	 * 修改房产信息
	 * url : /EstateManager/user/json-modifyEstate.do
	 * method : post
	 * @param estatebasicinfo
	 * */
	@RequestMapping(value = "json-modifyEstate")
	@ResponseBody
	public Result modifyEstate(Estatebasicinfo estatebasicinfo) {
		Result result;
		
		try {
			estateBasicInfoDao.update(estatebasicinfo);
			result = FactoryResult.getResult(FactoryResult.SUCCESS, estatebasicinfo, "修改数据成功");
		} catch (HibernateException e) {
			e.printStackTrace(System.out);
			result = FactoryResult.getResult(FactoryResult.EXCEPTION, e, "数据加载出错");
		}
		return result;
	}
	
	/**
	 * 删除房产信息
	 * url : /EstateManager/user/json-estateDelete.do
	 * method : post
	 * @param id
	 * 
	 * */
	@RequestMapping(value = "json-estateDelete", method = RequestMethod.POST)
	@ResponseBody
	public Result estateDelete(Integer id) {
		Result result;
		try {
			Estatebasicinfo estateBasicInfo = estateBasicInfoDao.get(id);
			if (estateBasicInfo != null) {
				estateBasicInfoDao.detete(estateBasicInfo);
				result = FactoryResult.getResult(FactoryResult.SUCCESS, null, "信息删除成功");
			}else {
				result = FactoryResult.getResult(FactoryResult.ERROR, null, "要删除的信息不存在");
			}
			
		} catch (HibernateException e) {
			e.printStackTrace(System.out);
			result = FactoryResult.getResult(FactoryResult.EXCEPTION, e, "信息删除失败");
		}
		return result;
	}
	
	/**
	 * 返回房产信息发布页面
	 * url : /EstateManager/user/view-publicInfoView.do
	 * method : get
	 * 
	 * */
	@RequestMapping(value = "view-publicInfoView", method = RequestMethod.GET)
	public String publicInfoView() {
		return "User/publicInfo";
	}
	
	/**
	 * 发布房屋租售信息
	 * url : /EstateManager/user/json-publicLeaseOrSaleInfo.do
	 * method : post
	 * @param leaseorsaleinfo
	 * 
	 * */
	@RequestMapping(value = "json-publicLeaseOrSaleInfo", method = RequestMethod.POST)
	@ResponseBody
	public Result publicLeaseOrSaleInfo(Leaseorsaleinfo leaseorsaleinfo, HttpServletRequest request) {
		Result result;
		leaseorsaleinfo.setPublishTime(Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
		leaseorsaleinfo.setStatue(LeaseOrSaleDao.STATUE_ONPUBLIC);
		try {
			 leaseOrSaleDao.save(leaseorsaleinfo);
			 Estatebasicinfo estatebasicinfo = estateBasicInfoDao.get(leaseorsaleinfo.getEstatebasicinfo().getId());
			 estatebasicinfo.setPublicStatu(EstateBasicInfoDao.PUBCLICSTATUE_PUBLISHED);
			 estateBasicInfoDao.update(estatebasicinfo);
			
			 result = FactoryResult.getResult(FactoryResult.SUCCESS, leaseorsaleinfo, "信息发布完成");
		} catch (Exception e) {
			e.printStackTrace(System.out);
			result = FactoryResult.getResult(FactoryResult.EXCEPTION, e, "系统错误");
		}
		return result;
	}

	/**
	 * 显示房产租售信息页面
	 * method : 
	 * url : /EstateManager/user/showPublickInfo.do
	 * */
	@RequestMapping(value = "showPublickInfo", method = RequestMethod.GET)
	public String showPublickInfo() {
		return "User/showPublic";
	}
	
	
	/**
	 * 获取房产租售信息数据页
	 * url : /EstateManager/user/getPublicInfo.do
	 * method : post
	 * @param pageSize
	 * @param pageNum
	 * */
	@RequestMapping(value = "getPublicInfo", method = RequestMethod.POST)
	@ResponseBody
	public Result getPublicInfo(Integer pageSize, Integer pageNum) {
		Result result;
		try {
			List<Leaseorsaleinfo> leaseorsaleinfos = leaseOrSaleDao.getListByStatu(pageSize, pageNum, LeaseOrSaleDao.STATUE_ONPUBLIC);
			for (Leaseorsaleinfo leaseorsaleinfo : leaseorsaleinfos) {
				Estatebasicinfo estatebasicinfo = (Estatebasicinfo)leaseorsaleinfo.getEstatebasicinfo().clone();
				estatebasicinfo.getUser().setPassword("");
				estatebasicinfo.getUser().setUsername("");
				leaseorsaleinfo.setEstatebasicinfo(estatebasicinfo);
			}
			String jsonString =  JSON.toJSONString(leaseorsaleinfos, SerializerFeature.DisableCircularReferenceDetect);
		//	System.out.println(jsonString);
			result = FactoryResult.getResult(FactoryResult.SUCCESS,jsonString, "获取数据成功");
		} catch (Exception e) {
			e.printStackTrace(System.out);
			result = FactoryResult.getResult(FactoryResult.EXCEPTION, e, "加载数据失败,系统异常");
		}
		return result;
	}
	
	/**
	 * 根据发布信息ID返回详情页
	 * url : /EstateManager/user/getPublicInfoDetailViewById.do
	 * method : post
	 * @param id
	 * */
	@RequestMapping(value = "getPublicInfoDetailViewById", method = RequestMethod.GET)
	public String getPublicInfoDetailViewById(Integer id, Model model){
			Leaseorsaleinfo leaseorsaleinfo = leaseOrSaleDao.get(id);
			if (leaseorsaleinfo != null) {
				model.addAttribute("leaseorsaleinfo", leaseorsaleinfo);
				Estatebasicinfo estatebasicinfo = estateBasicInfoDao.get(leaseorsaleinfo.getEstatebasicinfo().getId());
				model.addAttribute("estatebasicinfo", estatebasicinfo);
				model.addAttribute("user", userDao.get(estatebasicinfo.getUser().getId()));
				
			}
				
			return "User/estateDetail";
	}
	
	/**
	 * 返回获取已发布的房产租售信息页面
	 * url : /EstateManager/user/view-showPersonPublicInfo.do
	 * method : get
	 * 
	 **/
	@RequestMapping(value = "view-showPersonPublicInfo", method = RequestMethod.GET)
	public String showPpersonPublicInfo() {
		return "User/personalPublic";
	}
	
	
	
	
	/**
	 * 根据用户ID,发布信息完成状态,获取已发布的房产租售信息页
	 * url : /EstateManager/user/json-showPersonPublickInfo.do
	 * method : post
	 * @param userId
	 * @param pageSize
	 * @param pageNum
	 * @param statue
	 * */
	@RequestMapping(value = "json-showPersonPublickInfo", method = RequestMethod.POST)
	@ResponseBody
	public Result showPersonPublickInfo(Integer userId, String statue,int pageSize, int pageNum, HttpServletRequest request) {
		Result result;
		if (userId == null) {
			userId = (Integer)request.getSession().getAttribute("userId");
		}
		try {
			List<Leaseorsaleinfo> leaseorsaleinfos = leaseOrSaleDao.getUserLeasorsaleInfos(userId, statue,pageSize, pageNum);
			for (Leaseorsaleinfo leaseorsaleinfo : leaseorsaleinfos) {
				Estatebasicinfo estatebasicinfo = (Estatebasicinfo)leaseorsaleinfo.getEstatebasicinfo().clone();
				estatebasicinfo.getUser().setPassword("");
				estatebasicinfo.getUser().setUsername("");
				leaseorsaleinfo.setEstatebasicinfo(estatebasicinfo);
			}
			result = FactoryResult.getResult(FactoryResult.SUCCESS, JSON.toJSONString(leaseorsaleinfos, SerializerFeature.DisableCircularReferenceDetect), "获取数据成功");
		} catch (Exception e) {
			e.printStackTrace(System.out);
			result = FactoryResult.getResult(FactoryResult.EXCEPTION, e, "获取数据失败");
		}
		return result;
	}
	
	
	
	
	
	
	/**
	 * 添加预订信息
	 * url : /EstateManager/user/json-addSchedual.do
	 * methoud : post
	 * @param estateschedual
	 * */
	@RequestMapping(value = "json-addSchedual", method = RequestMethod.POST)
	@ResponseBody
	public Result addSchedual(Estateschedual estateschedual, HttpServletRequest request) {
		Result result;
		if (estateschedual.getUser() == null) {
			User user = new User();
			user.setId((Integer)request.getSession().getAttribute("userId"));
			estateschedual.setUser(user);
		}
		Leaseorsaleinfo leaseorsaleinfo = leaseOrSaleDao.get(estateschedual.getLeaseorsaleinfo().getId());
		if (leaseorsaleinfo.getMinPrice() > estateschedual.getSchedulePrice()) {
			result = FactoryResult.getResult(FactoryResult.ERROR, estateschedual, "报价低于预期价格");
		}else if (estateSchedualDao.getSechdualCount(estateschedual.getLeaseorsaleinfo().getId()) >= leaseorsaleinfo.getMaxScheduleAmount()) {
			result = FactoryResult.getResult(FactoryResult.ERROR, estateschedual, "预订人数已满");
		}
		else {
			estateschedual.setSchedualresult(EstateSchedualDao.SCHEDUAL_NOCHECK);
			estateschedual.setScheduleTime(Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
			try {
				estateSchedualDao.save(estateschedual);
				result = FactoryResult.getResult(FactoryResult.SUCCESS, estateschedual, "预订信息已添加");
			} catch (HibernateException e) {
				e.printStackTrace(System.out);
				result = FactoryResult.getResult(FactoryResult.EXCEPTION, e, "系统异常");
			}
		}
		return result;
	}
	
	/**
	 * 取消租售信息发布
	 * url : /EstateManager/user/json-cancelPublic.do
	 * method : post
	 * @param id 发布信息ID 
	 * 
	 * */
	@RequestMapping(value = "json-cancelPublic", method = RequestMethod.POST)
	@ResponseBody
	public Result cancelPublic(Integer id) {
		Result result;
		try {
			multiTableDao.cancelLeaseorsale(id);
			result = FactoryResult.getResult(FactoryResult.SUCCESS, null, "取消发布记录成功");
		} catch (HibernateException e) {
			e.printStackTrace(System.out);
			result = FactoryResult.getResult(FactoryResult.EXCEPTION, e, "系统错误");
		}
		return result;
	}
	
	/**
	 * 修改租售发布信息
	 * url : /EstateManager/user/json-modifyPublicInfo.do
	 * method : post
	 * @param leaseorsaleinfo
	 * 
	 * */
	@RequestMapping(value = "json-modifyPublicInfo", method = RequestMethod.POST )
	@ResponseBody
	public Result modifyPublicInfo(Leaseorsaleinfo leaseorsaleinfo) {
		Result result;
		try {
			Leaseorsaleinfo leaseorsaleinfoOld = leaseOrSaleDao.get(leaseorsaleinfo.getId());
			leaseorsaleinfoOld.setMaxScheduleAmount(leaseorsaleinfo.getMaxScheduleAmount());
			leaseorsaleinfoOld.setMinPrice(leaseorsaleinfo.getMinPrice());
			leaseorsaleinfoOld.setPublicDescribe(leaseorsaleinfo.getPublicDescribe());
			leaseorsaleinfoOld.setUnit(leaseorsaleinfo.getUnit());
			leaseOrSaleDao.update(leaseorsaleinfoOld);
			result = FactoryResult.getResult(FactoryResult.SUCCESS, null, "发布信息修改成功");
		} catch (HibernateException e) {	
			e.printStackTrace(System.out);
			result = FactoryResult.getResult(FactoryResult.EXCEPTION, e, "信息修改失败，系统异常");
		}
		return result;
	}
	
	/**
	 * 返回房产所有者的预订信息页面
	 * url : /EstateManager/user/view-showOnwerWchedual.do
	 * method : get
	 * */
	@RequestMapping(value = "view-showOnwerWchedual", method = RequestMethod.GET)
	public String showOnwerWchedual() {
		return "User/estateSchedual";
	}
	
	
	
	
	
	
	
	/**
	 * 根据房产所以人Id,预订结果获取预订信息
	 * url : /EstateManager/user/json-getSchedulByOwnerandResult.do
	 * method : post
	 * @param userId
	 * @param schedualResult
	 * @param pageSize
	 * @param PageNum
	 * 
	 * */
	@RequestMapping(value = "json-getSchedulByOwnerandResult", method = RequestMethod.POST)
	@ResponseBody
	public Result getSchedulByOwnerandResult(Integer userId, int pageSize, String schedualResult,int pageNum, HttpServletRequest request) {
		Result result;
		if (userId == null) {
			userId = (Integer)request.getSession().getAttribute("userId");
		}
		List<Estateschedual> estatescheduals = estateSchedualDao.getEstateschedualsByOwnerIdAndResrult(userId, schedualResult, pageSize, pageNum);
		if (estatescheduals != null) {
			try {
				for (Estateschedual estateschedual : estatescheduals) {
					Leaseorsaleinfo leaseorsaleinfo = (Leaseorsaleinfo)estateschedual.getLeaseorsaleinfo().clone();
					leaseorsaleinfo.getEstatebasicinfo().getUser().setUsername("");
					leaseorsaleinfo.getEstatebasicinfo().getUser().setPassword("");
					estateschedual.setLeaseorsaleinfo(leaseorsaleinfo);
				}
				result = FactoryResult.getResult(FactoryResult.SUCCESS, JSON.toJSONString(estatescheduals, SerializerFeature.DisableCircularReferenceDetect), "获取数据成功");
			} catch (Exception e) {
				e.printStackTrace(System.out);
				result = FactoryResult.getResult(FactoryResult.EXCEPTION, e, "获取数据失败,系统异常");
			}
			
		}else {
			result = FactoryResult.getResult(FactoryResult.ERROR, null, "数据不存在");
		}
		return result;
	}
	
	
	
	
	/**
	 * 根据房产所有人Id返回房产租售记录
	 * 
	 * 
	 * 
	 * 
	 * */
	public Result getOwnerLeaseOrsaleRecord(Integer ownerId, int pageSize, int pageNum,HttpServletRequest request) {
		if (ownerId == null) {
			ownerId = (Integer)request.getSession().getAttribute("userId");
		}
		Result result;
		List<Leaseorsalerecords> leaseorsalerecords = leaseOrSaleRecoldDao.getRecoldsByOwnerId(ownerId, pageSize, pageNum);
		if (leaseorsalerecords != null) {
			try {
				for (Leaseorsalerecords item : leaseorsalerecords) {
					User ownerUser = (User)item.getUserByOwnerId().clone();
					User buyer = (User)item.getUserByBuyerId().clone();
					Estatebasicinfo estatebasicinfo = (Estatebasicinfo)item.getEndTime().clone();
					ownerUser.setUsername("");
					ownerUser.setPassword("");
					buyer.setUsername("");
					buyer.setPassword("");
					estatebasicinfo.getUser().setUsername("");
					estatebasicinfo.getUser().setPassword("");
					item.setUserByBuyerId(buyer);
					item.setUserByOwnerId(ownerUser);
					item.setEstatebasicinfo(estatebasicinfo);
				}
				result = FactoryResult.getResult(FactoryResult.SUCCESS, JSON.toJSONString(leaseorsalerecords, SerializerFeature.DisableCircularReferenceDetect), "获取租售记录成功");
			} catch (Exception e) {
				e.printStackTrace();
				result = FactoryResult.getResult(FactoryResult.EXCEPTION, e, "获取记录失败,系统异常");
			}
		}else {
			result = FactoryResult.getResult(FactoryResult.ERROR, null, "记录不存在");
		}
		return result;
	}
	
	
	
	
}
